package com.qqt.csr.route.service.strategy.skillset;

import com.qqt.csr.common.utils.CacheKeyUtil;
import com.qqt.csr.common.utils.RedissonLockUtil;
import com.qqt.csr.route.dto.resp.SkillSetDTO;
import com.qqt.csr.route.entity.RouteBizRule;
import com.qqt.csr.route.entity.RouteTask;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * 技能组轮询分配策略
 */
@Slf4j
@Component
public class SkillSetPollDistribution implements ISkillSetDistribution {
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private RedissonLockUtil redissonLockUtil;

    @Override
    public SkillSetDTO distribution(RouteTask routeTask, List<SkillSetDTO> skillSetConfigList) {
        RouteBizRule routeBizRule = routeTask.getRouteBizRule();
        String cacheKey = String.format(CacheKeyUtil.CsrRoute.DISTRIBUTION_SKILL_SET_POLL_INDEX, routeBizRule.getId());
        redissonClient.getAtomicLong(cacheKey).expireIfSet(Duration.of(1, ChronoUnit.DAYS));
        long index = redissonClient.getAtomicLong(cacheKey).incrementAndGet();
        return skillSetConfigList.get((int) (index % skillSetConfigList.size()));
    }
}
